class Element
{
	public Element next = null;
	public int i;
	Element (int i)
	{
		this.i=i;
	}
}

public class StackOnLists 
{
	Element head = null;
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{ ");
		Element current = head;
		while (current != null)
		{
			sb.append (current.i + " ");
			current = current.next;
		}
		sb.append("}");
		return sb.toString();
	}
	
	public void push(int i)
	{
		Element elem = new Element(i);
		elem.next = head;
		head = elem;
	}
	
	public int pop()
	{
		int p = head.i;
		head = head.next;
		return p;
	}

	public static void main(String[] args) 
	{
		StackOnLists stack = new StackOnLists();
		
		System.out.println("\tPush in stack");
		
		for (int i=0; i<10; i++)
		{
			int k = (int)(Math.random()*100);
			stack.push(k);
		}
		System.out.println("Stack: " + stack);
		
		for (int i=0; i<10; i++)
		{
			int k = stack.pop();
			System.out.println("Pop " + k + " form stack " + stack);
		}
	}

}
